﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.ComponentModel;

namespace Billing.Windows
{
    public partial class TransferData : Window
    {
        private bool IsWorking = false;

        public TransferData()
        {
            InitializeComponent();
        }

        private void SetVisibilityCollapsed()
        {
            successBills.Visibility = successBillDetails.Visibility = successCakeDetails.Visibility = successCreditNotes.Visibility = successDamages.Visibility = successExpenses.Visibility = successAmounts.Visibility = successRemaningStock.Visibility = successVendorBills.Visibility = successSalaryDetails.Visibility = Visibility.Collapsed;
            failureAmounts.Visibility = failureBillDetails.Visibility = failureBills.Visibility = failureCakeDetails.Visibility = failureCreditNotes.Visibility = failureDamages.Visibility = failureExpenses.Visibility = failureRemainingStock.Visibility = failureVendorBills.Visibility = failureSalaryDetails.Visibility = Visibility.Collapsed;
            nodataAmounts.Visibility = nodataBillDetails.Visibility = nodataBills.Visibility = nodataCakeDetails.Visibility = nodataCreditNotes.Visibility = nodataDamages.Visibility = nodataExpenses.Visibility = nodataRemainingStock.Visibility = nodataVendorBills.Visibility = nodataSalaryDetails.Visibility = Visibility.Collapsed;
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.F1)
                {
                    CheckAvailability();
                }
                else if (e.Key == Key.F2)
                {
                    DataRemainderPopup data = new DataRemainderPopup();
                    data.Owner = this;
                    if (data.ShowDialog().Value)
                    {
                        bool isShutDown = false;
                        if (chkShutDown.IsChecked.Value)
                        {
                            if (MessageBoxResult.Yes == MessageBox.Show("Do you want to shut down after Transfer?", "Shut Down", MessageBoxButton.YesNo, MessageBoxImage.Question))
                            {
                                isShutDown = true;
                            }
                            else
                            {
                                isShutDown = false;
                            }
                        }
                        chkShutDown.IsChecked = isShutDown;
                        TransferCompleteData();
                        //Shut Down the machine forcefully
                        if (isShutDown)
                            System.Diagnostics.Process.Start("shutdown.exe", "-s -f -t 0");
                    }
                }
                else if (e.Key == Key.F3)
                {
                    chkShutDown.IsChecked = !chkShutDown.IsChecked;
                }
                else if (e.Key == Key.Escape)
                {
                    if (IsWorking)
                    {
                        MessageBox.Show("Transfering Data to Server. Don't Close", "Transfering Data", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception) { }
        }

        /*private void TransferCompleteData()
        {
            try
            {
                IsWorking = true;
                if (MessageBoxResult.OK == MessageBox.Show("Do You want", "", MessageBoxButton.OKCancel))
                {
                    BackgroundWorker workerBill = new BackgroundWorker();
                    workerBill.DoWork += (s, args) =>
                    {
                        //Bill Transfer




                        string msg = string.Empty;
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            msg = msgBills.Content.ToString();

                            if (msg == "Data Available to Transfer")
                            {
                                this.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    msgBills.Visibility = Visibility.Collapsed;
                                    prgBills.Visibility = Visibility.Visible;
                                }));

                                if (Database.TransferBillData() > 0)
                                {
                                    MessageBox.Show("Bill Data Transferred Successfully.", "Bill Data Success", MessageBoxButton.OK, MessageBoxImage.Information);
                                    this.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        msgBills.Content = "Success";
                                        msgBills.Visibility = Visibility.Visible;
                                        prgBills.Visibility = Visibility.Collapsed;
                                    }));
                                }
                                else
                                {
                                    MessageBox.Show("Some Unknown Error Has Occurred.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                                    this.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        msgBills.Content = "Failure";
                                        msgBills.Visibility = Visibility.Visible;
                                        prgBills.Visibility = Visibility.Collapsed;
                                    }));
                                }
                            }

                            //Bill Detail Transfer
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                msg = msgBillDetails.Content.ToString();
                            }));

                            if (msg == "Data Available to Transfer")
                            {
                                this.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    msgBillDetails.Visibility = Visibility.Collapsed;
                                    prgBillDetails.Visibility = Visibility.Visible;
                                }));

                                if (Database.TransferBillDetailData() > 0)
                                {
                                    MessageBox.Show("Bill Detail Data Transferred Successfully.", "Bill Detail Data Success", MessageBoxButton.OK, MessageBoxImage.Information);
                                    this.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        msgBillDetails.Content = "Success";
                                        prgBillDetails.Visibility = Visibility.Collapsed;
                                        msgBillDetails.Visibility = Visibility.Visible;
                                    }));
                                }
                                else
                                {
                                    MessageBox.Show("Some Unknown Error Has Occurred.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                                    this.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        msgBillDetails.Content = "Failure";
                                        prgBillDetails.Visibility = Visibility.Collapsed;
                                        msgBillDetails.Visibility = Visibility.Visible;
                                    }));
                                }
                            }

                        }));
                    };
                    workerBill.RunWorkerAsync();
                }
            }
            catch (Exception) { }
        }*/

        private void TransferCompleteData()
        {
            try
            {
                SetVisibilityCollapsed();

                //IsWorking = true;
                CheckAvailability();

                if (MessageBoxResult.OK == MessageBox.Show("Do You want to Transfer Data. Application might be HANGED for few minutes.", "Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question))
                {
                    //Bill Transfer
                    if (msgBills.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferBillData() > 0)
                        {
                            //MessageBox.Show("Bill Data Transferred Successfully.", "Bill Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successBills.Visibility = Visibility.Visible;
                            msgBills.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Bill Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureBills.Visibility = Visibility.Visible;
                            msgBills.Content = "Failure";
                        }
                    }

                    //Bill Detail Transfer
                    if (msgBillDetails.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferBillDetailData() > 0)
                        {
                            //MessageBox.Show("Bill Detail Data Transferred Successfully.", "Bill Detail Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successBillDetails.Visibility = Visibility.Visible;
                            msgBillDetails.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Bill Details Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureBillDetails.Visibility = Visibility.Visible;
                            msgBillDetails.Content = "Failure";
                        }
                    }

                    //Cake Details Copy
                    if (msgCakeDetails.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.CopyCakeDetailData() > 0) //&& Database.TransferCakeBalance() > 0)
                        {
                            //MessageBox.Show("Cake Data Transferred Successfully.", "Cake Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successCakeDetails.Visibility = Visibility.Visible;
                            msgCakeDetails.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Cakes Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureCakeDetails.Visibility = Visibility.Visible;
                            msgCakeDetails.Content = "Failure";
                        }
                    }

                    //Credit Notes Transfer
                    if (msgCreditNotes.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferCreditNotesData() > 0)
                        {
                            //MessageBox.Show("Credit Notes Data Transferred Successfully.", "Credit Notes Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successCreditNotes.Visibility = Visibility.Visible;
                            msgCreditNotes.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Credit Notes Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureCreditNotes.Visibility = Visibility.Visible;
                            msgCreditNotes.Content = "Failure";
                        }
                    }

                    //Damages Transfer
                    if (msgDamages.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferDamagesData() > 0)
                        {
                            //MessageBox.Show("Damages Data Transferred Successfully.", "Damages Data Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successDamages.Visibility = Visibility.Visible;
                            msgDamages.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Damages Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureDamages.Visibility = Visibility.Visible;
                            msgDamages.Content = "Failure";
                        }
                    }

                    //Expenses Transfer
                    if (msgExpenses.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferExpensesData() > 0)
                        {
                            //MessageBox.Show("Expenses Data Transferred Successfully.", "Expenses Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successExpenses.Visibility = Visibility.Visible;
                            msgExpenses.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureExpenses.Visibility = Visibility.Visible;
                            msgExpenses.Content = "Failure";
                        }
                    }

                    //Amounts Transfer
                    if (msgAmounts.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferAmountsData() > 0)
                        {
                            //MessageBox.Show("Sodexho / Credit Cards Amounts Data Transferred Successfully.", "Amounts Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            successAmounts.Visibility = Visibility.Visible;
                            msgAmounts.Content = "Success";
                        }
                        else
                        {
                            //MessageBox.Show("Some Unknown Error Has Occurred While Transfering Amounts Data.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                            failureAmounts.Visibility = Visibility.Visible;
                            msgAmounts.Content = "Failure";
                        }
                    }

                    //Taking Remaining Stock Snap Shot to local database
                    //MessageBox.Show("1");
                    try
                    {
                        Database.RemainingStockSnapShotToLocal();
                    }
                    catch (Exception) { }

                    //MessageBox.Show("2");
                    try
                    {
                        Database.TransferSnapShotData();
                    }
                    catch (Exception) { }

                    //MessageBox.Show("3");
                    //Remaining Stock Transfer
                    if (Database.CopyRemainingStock() > 0)
                    {
                        //MessageBox.Show("Remaining Stock Data Transferred Successfully.", "Remaining Stock Data Transfer Success", MessageBoxButton.OK, MessageBoxImage.Information);
                        successRemaningStock.Visibility = Visibility.Visible;
                        msgRemainingStock.Content = "Success";
                    }
                    else
                    {
                        MessageBox.Show("Some Unknown Error Has Occurred While Transfering Remaining Stock.", "Error Has Occurred", MessageBoxButton.OK, MessageBoxImage.Error);
                        failureRemainingStock.Visibility = Visibility.Visible;
                        msgRemainingStock.Content = "Failure";
                    }

                    //Bulk Orders Copy
                    if (msgBulkOrder.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.CopyBulkOrdersData() > 0)
                        {
                            successBulkOrder.Visibility = Visibility.Visible;
                            msgBulkOrder.Content = "Success";
                        }
                        else
                        {
                            failureBulkOrder.Visibility = Visibility.Visible;
                            msgBulkOrder.Content = "Failure";
                        }
                    }

                    //Vendor Bills Transfer
                    if (msgVendorBills.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferVendorBillsData() > 0)
                        {
                            successVendorBills.Visibility = Visibility.Visible;
                            msgVendorBills.Content = "Success";
                        }
                        else
                        {
                            failureVendorBills.Visibility = Visibility.Visible;
                            msgVendorBills.Content = "Failure";
                        }
                    }

                    //Salaries Details
                    if (msgSalaryDetails.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferSalariesData() > 0)
                        {
                            successSalaryDetails.Visibility = Visibility.Visible;
                            msgSalaryDetails.Content = "Success";
                        }
                        else
                        {
                            failureSalaryDetails.Visibility = Visibility.Visible;
                            msgSalaryDetails.Content = "Failure";
                        }
                    }

                    /*//Login Details
                    if (msgLoginDetails.Content.ToString() == "Data Available to Transfer")
                    {
                        if (Database.TransferLoginDetails() > 0)
                        {
                            successLoginDetails.Visibility = Visibility.Visible;
                            msgLoginDetails.Content = "Success";
                        }
                        else
                        {
                            failureLoginDetails.Visibility = Visibility.Visible;
                            msgLoginDetails.Content = "Failure";
                        }
                    }*/

                    //Stock Transaction Details
                    Database.TransferStockTransactionDetails();

                    MessageBox.Show("Data Transfer Completed. Refer Transfer Data Screen for Results.", "Data Transfer Completed", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Some Exception Has Occurred. Please try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void CheckAvailability()
        {
            try
            {
                prgBills.Visibility = Visibility.Collapsed;
                msgBills.Visibility = Visibility.Visible;
                msgBillDetails.Visibility = Visibility.Visible;
                prgCakeDetails.Visibility = Visibility.Collapsed;
                msgCakeDetails.Visibility = Visibility.Visible;
                msgBulkOrder.Visibility = Visibility.Visible;


                //Bill
                if (Database.CheckBillsAvailability())
                {
                    msgBills.Content = "Data Available to Transfer";
                }
                else
                {
                    msgBills.Content = "No Data";
                    nodataBills.Visibility = Visibility.Visible;
                    successBills.Visibility = failureBills.Visibility = Visibility.Collapsed;
                }

                //Bill Detail
                if (Database.CheckBillDetailsAvailability())
                {
                    msgBillDetails.Content = "Data Available to Transfer";
                }
                else
                {
                    msgBillDetails.Content = "No Data";
                    nodataBillDetails.Visibility = Visibility.Visible;
                    successBillDetails.Visibility = failureBillDetails.Visibility = Visibility.Collapsed;
                }

                //Cakes
                if (Database.CheckCakeBillsAvailability())
                {
                    msgCakeDetails.Content = "Data Available to Transfer";
                }
                else
                {
                    msgCakeDetails.Content = "No Data";
                    nodataCakeDetails.Visibility = Visibility.Visible;
                    successCakeDetails.Visibility = failureCakeDetails.Visibility = Visibility.Collapsed;
                }

                //CreditNotes
                if (Database.CheckCreditNotesAvailability())
                {
                    msgCreditNotes.Content = "Data Available to Transfer";
                }
                else
                {
                    msgCreditNotes.Content = "No Data";
                    nodataCreditNotes.Visibility = Visibility.Visible;
                    successCreditNotes.Visibility = failureCreditNotes.Visibility = Visibility.Collapsed;
                }

                //Damages
                if (Database.CheckDamagesAvailability())
                {
                    msgDamages.Content = "Data Available to Transfer";
                }
                else
                {
                    msgDamages.Content = "No Data";
                    nodataDamages.Visibility = Visibility.Visible;
                    successDamages.Visibility = failureDamages.Visibility = Visibility.Collapsed;
                }

                //Expenses
                if (Database.CheckExpensesAvailability())
                {
                    msgExpenses.Content = "Data Available to Transfer";
                }
                else
                {
                    msgExpenses.Content = "No Data";
                    nodataExpenses.Visibility = Visibility.Visible;
                    successExpenses.Visibility = failureExpenses.Visibility = Visibility.Collapsed;
                }

                //Amounts
                if (Database.CheckAmountsAvailability())
                {
                    msgAmounts.Content = "Data Available to Transfer";
                }
                else
                {
                    msgAmounts.Content = "No Data";
                    nodataAmounts.Visibility = Visibility.Visible;
                    successAmounts.Visibility = failureAmounts.Visibility = Visibility.Collapsed;
                }

                //Remaining Stock
                msgRemainingStock.Content = "Data Available to Transfer";

                //Bulk Orders
                if (Database.CheckBulkOrdersAvailability())
                {
                    msgBulkOrder.Content = "Data Available to Transfer";
                }
                else
                {
                    msgBulkOrder.Content = "No Data";
                    nodataBulkOrder.Visibility = Visibility.Visible;
                    successBulkOrder.Visibility = failureAmounts.Visibility = Visibility.Collapsed;
                }

                //Vendor Bills
                if (Database.CheckVendorBillsAvailability())
                {
                    msgVendorBills.Content = "Data Available to Transfer";
                }
                else
                {
                    msgVendorBills.Content = "No Data";
                    nodataVendorBills.Visibility = Visibility.Visible;
                    successVendorBills.Visibility = failureVendorBills.Visibility = Visibility.Collapsed;
                }

                //Salary Details
                if (Database.CheckSalaryDetailsAvailability())
                {
                    msgSalaryDetails.Content = "Data Available to Transfer";
                }
                else
                {
                    msgSalaryDetails.Content = "No Data";
                    nodataSalaryDetails.Visibility = Visibility.Visible;
                    successSalaryDetails.Visibility = failureSalaryDetails.Visibility = Visibility.Collapsed;
                }

                /*//Login Details
                if (Database.CheckLoginDetailsAvailability())
                {
                    msgLoginDetails.Content = "Data Available to Transfer";
                }
                else
                {
                    msgLoginDetails.Content = "No Data";
                    nodataLoginDetails.Visibility = Visibility.Visible;
                    successLoginDetails.Visibility = failureLoginDetails.Visibility = Visibility.Collapsed;
                }*/
            }
            catch (Exception)
            {
                MessageBox.Show("Some Exception Has Occurred. Please try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
